package com.qc.bus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.bus.dao.BusGoodsDao;
import com.qc.bus.dao.BusProviderDao;
import com.qc.bus.entity.BusGoods;
import com.qc.bus.entity.BusProvider;
import com.qc.bus.service.BusGoodsService;
import com.qc.bus.service.BusProviderService;
import com.qc.bus.vo.GoodsVo;
import com.qc.sys.common.Constant;
import com.qc.sys.common.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * (BusGoods)表服务实现类
 *
 * @author AltmanQccc
 * @since 2020-09-09 14:28:31
 */
@Service
@EnableCaching
@Transactional
public class BusGoodsServiceImpl extends ServiceImpl<BusGoodsDao, BusGoods> implements BusGoodsService {

    @Autowired
    private BusGoodsDao goodsDao;

    @Autowired
    private BusProviderService providerService;

    @Override
    public DataGridView queryAllGoods(GoodsVo goodsVo) {
        IPage<BusGoods> page = new Page<>(goodsVo.getPage(),goodsVo.getLimit());
        QueryWrapper<BusGoods> qw = new QueryWrapper<>();
        qw.eq(goodsVo.getAvailable()!=null,"available",goodsVo.getAvailable());
        qw.eq(goodsVo.getProviderid()!=null,"providerid",goodsVo.getProviderid());
        qw.like(StringUtils.isNotBlank(goodsVo.getGoodsname()),"goodsname",goodsVo.getGoodsname());
        qw.like(StringUtils.isNotBlank(goodsVo.getSize()),"size",goodsVo.getSize());
        qw.like(StringUtils.isNotBlank(goodsVo.getProduceplace()),"produceplace",goodsVo.getProduceplace());
        qw.like(StringUtils.isNotBlank(goodsVo.getProductcode()),"productcode",goodsVo.getProductcode());
        qw.like(StringUtils.isNotBlank(goodsVo.getPromitcode()),"promitcode",goodsVo.getPromitcode());
        qw.like(StringUtils.isNotBlank(goodsVo.getDescription()),"description",goodsVo.getDescription());
        goodsDao.selectPage(page,qw);
        List<BusGoods> records = page.getRecords();
        for (BusGoods record : records) {
            if(null!=record.getProviderid()){
                BusProvider provider = providerService.getById(record.getProviderid());
                record.setProvidername(provider.getProvidername());
            }
        }

        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 查询可用的商品信息,不分页
     * */
    @Override
    public DataGridView queryAllAvailableGoods() {
        QueryWrapper<BusGoods> qw = new QueryWrapper<>();
        qw.eq("available", Constant.USER_AVAILABLE_TRUE);
        List<BusGoods> busGoods = this.goodsDao.selectList(qw);
        return new DataGridView(busGoods);
    }

    @CachePut(cacheNames = "BusGoodsServiceImpl",key = "#result.id")
    @Override
    public BusGoods InsertGoods(BusGoods goods) {
        this.goodsDao.insert(goods);
        return goods;
    }

    @CachePut(cacheNames = "BusGoodsServiceImpl",key = "#result.id")
    @Override
    public BusGoods UpdateGoods(BusGoods goods) {
        BusGoods busGoods = this.goodsDao.selectById(goods.getId());
        BeanUtil.copyProperties(goods,busGoods, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        this.goodsDao.updateById(busGoods);
        return busGoods;
    }


    @Cacheable(cacheNames = "BusGoodsServiceImpl",key = "#id")
    @Override
    public BusGoods getById(Serializable id) {
        return super.getById(id);
    }

    @CacheEvict(cacheNames = "BusGoodsServiceImpl",key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    @Override
    public DataGridView queryGoodsByProviderId(Integer providerid) {
        if (null==providerid){
            return new DataGridView();
        }
        QueryWrapper<BusGoods> qw = new QueryWrapper<>();
        qw.eq("available", Constant.USER_AVAILABLE_TRUE);
        qw.eq("providerid",providerid);
        List<BusGoods> busGoods = this.goodsDao.selectList(qw);
        return new DataGridView(busGoods);
    }
}